#!user/bin/env python3
# -- coding: utf-8 --
from lxml import etree
import requests
import json
import os

# 1、建文件夹
if not os.path.exists('./testdata'):
    os.mkdir('./testdata')
for maxpage in range(3):
    url = f'https://www.ximalaya.com/revision/album/v1/getTracksList?albumId=9742296&pageNum={maxpage + 1}&sort=0'
    user_agent = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36'}
    # 2.获取所有标题和trackId
    r = requests.get(url=url, headers=user_agent).text
    r = json.loads(r)
    lists = r['data']['tracks']
    # print(lists)
    trackId = [dicts['trackId'] for dicts in lists]
    title = [dicts['title'] for dicts in lists]
    dicts = dict(zip(trackId, title))
    # 用trackId拼接出没有音频的网址
    for trackId, title in dicts.items():
        url = f'https://www.ximalaya.com/revision/play/v1/audio?id={trackId}&ptype=1'
        r = requests.get(url=url, headers=user_agent).text
        r = json.loads(r)
        src = r['data']['src']
        m4a = src.split('.')[-1]
        path = f'./testdata/{title}.{m4a}'
        r = requests.get(url=src).content
        # 写入
        with open(path, 'wb') as file:
            file.write(r)
            print(f'{title} 下载成功！！！')
